package com.bringway.p2p.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.util.StringUtils;
import com.bringway.p2p.service.JhnhkService;

import net.sf.json.JSONObject;
/**  
 * @类名 JhnhkController  
 * @功能 TODO  计划内还款管理
 * @作者 js 
 * @日期 2016年8月5日 上午9:38:15  
 *    
 */
@Controller
@RequestMapping("/jhnhk")
public class JhnhkController extends JsonController{
	Logger logger=Logger.getLogger(JhnhkController.class);
	@Autowired
	private JhnhkService jhnhkService;
	/**
	 * @作者 js  
	 * @方法名 queryJhnhkList  
	 * @功能 TODO 查询 应还款列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryJhnhkList")
	public ModelAndView queryJhnhkList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		SysUserDetails sysUserDetails=(SysUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		
		String c_xmmc = RequestUtils.getStringParameter(request, "c_xmmc");
		String c_zqmc = RequestUtils.getStringParameter(request, "c_zqmc");
		String c_jkr = RequestUtils.getStringParameter(request, "c_jkr");
		String c_gyh = RequestUtils.getStringParameter(request, "c_gyh");
		String c_xmzt = RequestUtils.getStringParameter(request, "c_xmzt");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("c_xmmc", c_xmmc);
		params.put("c_zqmc", c_zqmc);
		params.put("c_jkr", c_jkr);
		params.put("dlsjhm", sysUserDetails.getGyh());
		params.put("c_xmzt", c_xmzt);
		params.put("c_gyh", c_gyh);
		List<Map<String, Object>> list=jhnhkService.queryJhnMjqDkList(params);
		int count=jhnhkService.getJhnJhnMjqDkCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 saveJhnhk  
	 * @功能 TODO 保存 本金还款信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveJhnhk")
	public ModelAndView saveJhnhk(HttpServletRequest request,HttpServletResponse response) throws Exception {
		SysUserDetails sysUserDetails=(SysUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String jkryhm=RequestUtils.getStringParameter(request, "jkryhm");
		//String sjhm=RequestUtils.getStringParameter(request, "sjhm");
		String xmid=RequestUtils.getStringParameter(request, "xmid");
		String cgid=RequestUtils.getStringParameter(request, "cgid");
		String zqid=RequestUtils.getStringParameter(request, "zqid");
		String hkje=RequestUtils.getStringParameter(request, "hkje");
		//String hkje_bj=RequestUtils.getStringParameter(request, "hkje_bj");
		//String hkje_sy=RequestUtils.getStringParameter(request, "hkje_sy");
		String fpje=RequestUtils.getStringParameter(request, "fpje");
		String fbje=RequestUtils.getStringParameter(request, "fbje");
		String zhkyje=RequestUtils.getStringParameter(request, "zhkyje");
		String hkzl=RequestUtils.getStringParameter(request, "hkzl");
		result.put("success", true);
		if (null==sysUserDetails){
			logger.info("saveJhnhk 获取登陆用户名失败!");
			result.put("isAddSuc", false);
			result.put("msg", "获取登陆用户名失败,请重新登陆!");
			serialize(request, response, result);
			return null;
		}
		params.put("dlsjhm", sysUserDetails.getGyh());
		params.put("jkryhm", jkryhm);
		params.put("xmid", xmid);
		params.put("cgid", cgid);
		params.put("zqid", zqid);
		params.put("hkje", hkje);
		params.put("fbje", fbje);
		params.put("fpje", fpje);
		params.put("zhkyje", zhkyje);
		params.put("hkzl", hkzl);
		if (StringUtils.isNotEmpty(hkje)) {
			//验证是否偿还了本金
			params.put("hkzt", null);
			Map<String, Object> yhjeMap=jhnhkService.getJhnhkJe(params);
			if (StringUtils.isNotEmpty(MapUtils.getString(yhjeMap, "YHKJE"))) {
				logger.info("saveJhnhk 本息已经偿还!");
				result.put("isAddSuc", false);
				result.put("msg", "本息已经偿还,请勿重复操作!");
				serialize(request, response, result);
				return null;
			}
			//验证账户余额是否不小于还款金额
			Map<String, Object> jbxxMap=jhnhkService.getJbxxInfo(params);
			if (MapUtils.isNotEmpty(jbxxMap)&&jbxxMap.containsKey("KYJE")) {
				params.putAll(jbxxMap);
				if (Double.valueOf(hkje)>MapUtils.getDouble(jbxxMap, "KYJE")) {
					logger.info("saveJhnhk 本次还款金额大于账户可用金额!");
					result.put("isAddSuc", false);
					result.put("msg", "账户余额不足,请充值!");
					serialize(request, response, result);
					return null;
				}
			}
		} else {
			logger.info("saveJhnhk 偿还金额为空!");
			result.put("isAddSuc", false);
			result.put("msg", "偿还金额为空,请重新操作!");
			serialize(request, response, result);
			return null;
		}
		params.put("hkzt", "03");
		Map<String, Object> map=jhnhkService.saveJhnhk(params);
		result.putAll(map);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 getJhnhksy  
	 * @功能 TODO 获取 已成功还款信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getJhnhksy")
	public ModelAndView getJhnhksy(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String xmid=RequestUtils.getStringParameter(request, "xmid");
		String zqid=RequestUtils.getStringParameter(request, "zqid");
		String hkzl=RequestUtils.getStringParameter(request, "hkzl");
		params.put("xmid", xmid);
		params.put("zqid", zqid);
		params.put("hkzl", hkzl);
		params.put("hkzt", "01");
		Map<String, Object> yhjeMap=jhnhkService.getJhnhkJe(params);
		String yhsy=MapUtils.getString(yhjeMap, "YHKJE");
		if (StringUtils.isNotBlank(yhsy)) {
			result.put("yhsy", yhsy);
		}
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 saveJhnhksy  
	 * @功能 TODO 保存 收益还款信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	/*@RequestMapping("/saveJhnhksy")
	public ModelAndView saveJhnhksy(HttpServletRequest request,HttpServletResponse response) throws Exception {
		SysUserDetails sysUserDetails=(SysUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String jkryhm=RequestUtils.getStringParameter(request, "jkryhm");
		//String sjhm=RequestUtils.getStringParameter(request, "sjhm");
		String xmid=RequestUtils.getStringParameter(request, "xmid");
		String cgid=RequestUtils.getStringParameter(request, "cgid");
		String zqid=RequestUtils.getStringParameter(request, "zqid");
		String hkje=RequestUtils.getStringParameter(request, "hkje");
		String fpje=RequestUtils.getStringParameter(request, "fpje");
		String zhkyje=RequestUtils.getStringParameter(request, "zhkyje");
		String sy_yhsy=RequestUtils.getStringParameter(request, "sy_yhsy");
		
		String hkzl=RequestUtils.getStringParameter(request, "hkzl");
		result.put("success", true);
		if (null==sysUserDetails){
			logger.info("saveJhnhk 获取登陆用户名失败!");
			result.put("isAddSuc", false);
			result.put("msg", "获取登陆用户名失败,请重新登陆!");
			serialize(request, response, result);
			return null;
		}
		//params.put("gyh", sysUserDetails.getGyh());
		params.put("dlsjhm", sysUserDetails.getGyh());
		params.put("xmid", xmid);
		params.put("cgid", cgid);
		params.put("zqid", zqid);
		params.put("hkje", hkje);
		params.put("fpje", fpje);
		params.put("zhkyje", zhkyje);
		params.put("hkzl", hkzl);
		params.put("jkryhm", jkryhm);
		if (StringUtils.isNotEmpty(hkje)) {
			//验证收益是否已还足
			double b_hkje=Double.valueOf(hkje);
			double syyhsy=0;
			if (StringUtils.isNotEmpty(sy_yhsy)) {
				syyhsy=Double.valueOf(sy_yhsy);
			}
			if (b_hkje>syyhsy) {
				logger.info("saveJhnhk 还款金额不能大于剩余应还金额!");
				result.put("isAddSuc", false);
				result.put("msg", "还款金额不能大于剩余应还金额!");
				serialize(request, response, result);
				return null;
			}
			//验证账户余额是否不小于还款金额
			Map<String, Object> jbxxMap=jhnhkService.getJbxxInfo(params);
			if (MapUtils.isNotEmpty(jbxxMap)&&jbxxMap.containsKey("KYJE")) {
				params.putAll(jbxxMap);
				if (Double.valueOf(hkje)>MapUtils.getDouble(jbxxMap, "KYJE")) {
					logger.info("saveJhnhk 本次还款金额大于账户可用金额!");
					result.put("isAddSuc", false);
					result.put("msg", "账户余额不足,请充值!");
					serialize(request, response, result);
					return null;
				}
			}
		} else {
			logger.info("saveJhnhk 还款金额为空!");
			result.put("isAddSuc", false);
			result.put("msg", "还款金额为空,请重新操作!");
			serialize(request, response, result);
			return null;
		}
		Map<String, Object> map=jhnhkService.saveJhnhk(params);
		result.putAll(map);
		serialize(request, response, result);
		return null;
	}*/
	/**
	 * @作者 js  
	 * @方法名 queryJhnhkmxList  
	 * @功能 TODO 查询 还款明细列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryJhnhkmxList")
	public ModelAndView queryJhnhkmxList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		
		String q_xmmc=RequestUtils.getStringParameter(request, "q_xmmc");
		String q_xmbh=RequestUtils.getStringParameter(request, "q_xmbh");
		String q_zqmc=RequestUtils.getStringParameter(request, "q_zqmc");
		String q_gyh=RequestUtils.getStringParameter(request, "q_gyh");
		String q_hkzt=RequestUtils.getStringParameter(request, "q_hkzt");
		String q_hkzl=RequestUtils.getStringParameter(request, "q_hkzl");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("q_xmmc", q_xmmc);
		params.put("q_xmbh", q_xmbh);
		params.put("q_zqmc", q_zqmc);
		params.put("q_gyh", q_gyh);
		params.put("q_hkzt", q_hkzt);
		params.put("q_hkzl", q_hkzl);
		List<Map<String, Object>> list=jhnhkService.queryJhnhkmxList(params);
		int count=jhnhkService.getJhnhkmxCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/queryJhnhkzqList")
	public ModelAndView queryJhnhkzqList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String xmid=RequestUtils.getStringParameter(request, "xmid");
		params.put("xmid", xmid);
		List<Map<String, Object>> list=jhnhkService.queryJhnhkzqList(params);
		int count=jhnhkService.getJhnhkzqCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}
}
